<?php

/**
 * 
 * @author NutMotion <simonotm.linux@gmail.com> 
 */
//echo "dirname(__FILE__)=".dirname(__FILE__);
//echo "realpath(dirname(__FILE__))=".realpath(dirname(__FILE__));
//echo "realpath(dirname(__FILE__) . '/../includes/dbconn.inc') = ".

require_once(dirname(__FILE__) . "/../commons/common-funcs.php");
require_once(dirname(__FILE__) . "/../db/common-operations.php");
require_once(dirname(__FILE__) . "/../db/tagsOperations.php");

//we control the presence of the required parameters 
//we'll leave (critical) further controls on the values to a future version

if (!isset($_POST['action'])) {
//    $what=buildHtmlPresentationForError(__FILE__, __FUNCTION__, buildJSONErrorString($_POST)." Required parameter 'action' is missing. Can't process the request...");
    echo buildHtmlPresentationForError(__FILE__, __FUNCTION__, " Required parameter 'action' is missing. Can't process the request...");
    exit();
}

$action = $_POST['action'];

if (strcasecmp($action, "read") == 0) {
    echo handleReadOperation($_POST);
} else if (strcasecmp($action, "create") == 0) {
    echo buildHtmlPresentationForError(__FILE__, __FUNCTION__, " The action '$action' is not supported for the moment...");
} else {
    echo buildHtmlPresentationForError(__FILE__, __FUNCTION__, " Invalid value '$action' for parameter 'action'...");
}

// returns either the JSON result if the operation was successful, or an error msg with details about the cause of failure
function handleReadOperation($request) {


    if (isSet($request['id'])) {

        connect();
        $allResults = dbGetTagsOfId($request['id']);

        echo json_encode($allResults);
    } else {
        connect();
        
        $allResults=array();
        $allResults['tags'] = dbGetAllFromTags();

        echo json_encode($allResults);
    }
}

?>
